<?php

/**
 * Subclass for performing query and update operations on the 'ringtone_category' table.
 *
 * 
 *
 * @package lib.model
 */ 
class RingtoneCategoryPeer extends BaseRingtoneCategoryPeer
{
    public static function getRingtoneCount($nodeId) {
        $criteria =  new Criteria();
        $criteria->add( RingtonePeer::CATEGORY_ID, $nodeId );
        return RingtonePeer::doCount($criteria);
    }

    public static function getChildrenCount($nodeId) {
        $criteria = new Criteria();

		$criteria->add(self::PARENT_ID, $nodeId);
        $criteria->addAnd(self::ID, $nodeId, Criteria::NOT_EQUAL);

		return self::doCount($criteria);
    }

    public static function getListParents() {
        $criteria = new Criteria();

		$criteria->add(self::PARENT_ID, self::PARENT_ID."=".self::ID, Criteria::CUSTOM);
		$criteria->add(self::ENABLED,1,Criteria::EQUAL);// thangtv2 add
		$criteria->addAscendingOrderByColumn(self::WEB_ORDER);// thangtv2 add

		$v = self::doSelect($criteria);

		return !empty($v) > 0 ? $v : null;
    }

    public static function getListChildren($nodeId) {
        $criteria = new Criteria();

		$criteria->add(self::PARENT_ID, $nodeId);
		$criteria->add(self::ENABLED,1,Criteria::EQUAL);// thangtv2 add
		$criteria->addAscendingOrderByColumn(self::WEB_ORDER);// thangtv2 add
        $criteria->addAnd(self::ID, $nodeId, Criteria::NOT_EQUAL);
		
		$v = self::doSelect($criteria);

		return !empty($v) > 0 ? $v : null;
    }

    public static function getListCategory() {
        $categoryList = array();
        
        $parentList = self::getListParents();

        if(!$parentList) return null;

        foreach($parentList as $parent)
        {
            array_push($categoryList, $parent);
            $childrenList = self::getListChildren($parent->getId());
            if($childrenList)
            {
                foreach($childrenList as $child)
                {
                    $child->setName(".....".$child->getName());
                    array_push($categoryList, $child);
                }
            }
        }

        return $categoryList;
    }
}
